package cn.project.engineering.ui.adapter

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.EditText
import android.widget.Spinner
import android.widget.Toast
import cn.project.engineering.R
import cn.project.engineering.http.bean.DianjiaTwonfo
import cn.project.engineering.utils.toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class DianjianTwoDapter(layoutid: Int, data: MutableList<DianjiaTwonfo>) : BaseQuickAdapter<DianjiaTwonfo, BaseViewHolder>(layoutid,data)  {
     override fun convert(holder: BaseViewHolder, item: DianjiaTwonfo) {
         holder.setText(R.id.left_bottom_number,item.ItemNO)
         holder.setText(R.id.left_bottom_xm,item.ItemName)
         holder.setText(R.id.left_bottom_nr,item.ItemContent)

         holder.setText(R.id.left_bottom_jguo,item.Result)
         holder.setText(R.id.left_bottom_lx,item.ItemType)

         when(item.ItemType){
             "数值" -> {
                 holder.setVisible(R.id.left_bottom_nr,true)
                 holder.setGone(R.id.left_bottom_nr_text,true)
                 holder.setGone(R.id.sp1,true)

                 holder.getView<EditText>(R.id.left_bottom_nr).addTextChangedListener(object :TextWatcher{
                     override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                         if (item.DownValue.equals("")){
                             return
                         }
                         if (p0.toString().equals("")){
                             return
                         }
                             item.ItemContent=p0.toString()+""
                         val zp = p0.toString().toInt();
                         if ( zp in item.UpValue.toInt().. item.DownValue.toInt()){
                             item.Result="正常"
                             holder.setText(R.id.left_bottom_jguo,"正常")

                         }else{
                             item.Result="不正常"
                             holder.setText(R.id.left_bottom_jguo,"不正常")

                         }


                     }

                     override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {





                     }

                     override fun afterTextChanged(p0: Editable?) {
                     }
                 })

             }
             "选项" -> {
                 holder.setVisible(R.id.sp1,true)
                 holder.setGone(R.id.left_bottom_nr_text,true)
                 holder.setGone(R.id.left_bottom_nr,true)

                 var  spinner=holder.getView<Spinner>(R.id.sp1);
                 spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                     override fun onItemSelected(adapterView: AdapterView<*>?, view: View, i: Int, l: Long) {
                         item.Result=context.resources.getStringArray(R.array.spinner_arr)[i]
                         item.ItemContent=context.resources.getStringArray(R.array.spinner_arr)[i]
                         holder.setText(R.id.left_bottom_jguo,context.resources.getStringArray(R.array.spinner_arr)[i])

//                         Toast.makeText(context,""+i+"--"+l,  Toast.LENGTH_SHORT).show()
                     }
                     override fun onNothingSelected(adapterView: AdapterView<*>?) {}
                 }
             }
             "文本" -> {
                 holder.setVisible(R.id.left_bottom_nr_text,true)
                 holder.setGone(R.id.left_bottom_nr,true)
                 holder.setGone(R.id.sp1,true)
                 holder.getView<EditText>(R.id.left_bottom_nr_text).addTextChangedListener(object :TextWatcher{
                     override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                         item.ItemContent=p0.toString()+""
                         item.Result=p0.toString()+""
                         holder.setText(R.id.left_bottom_jguo,p0.toString()+"")



                     }

                     override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {


                     }

                     override fun afterTextChanged(p0: Editable?) {
                     }
                 })
             }
         }





     }

 }